package com.smart.home.netty.handler;

import com.smart.home.util.ResultCodeResolver;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;

/**
 * 执行输出按钮命令
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-10
 */
public class WireOutputCommandHandler extends AbstractChannelInboundHandler {

    public WireOutputCommandHandler(String command) {
        setCommand(command);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        String result = ByteBufUtil.hexDump(msg);
        setStatus(ResultCodeResolver.wireOutputResultStatus(result));
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //TODO 处理异常信息
        ctx.close();
    }
}
